import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/bad_event/ui/bad_event_list_page.dart';
import 'package:ls_work/emergency_event/ui/edit_emergency_event_page.dart';
import 'package:ls_work/emergency_event/view_model/emergency_event_detail_view_model.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../widgets/common_widget.dart';
class EmergencyEventDetailPage extends BaseStatefulMvvmWidget{
  dynamic id;

  EmergencyEventDetailPage({required this.id});

  @override
  State<StatefulWidget> createState() => _EmergencyEventDetailState();

}

class _EmergencyEventDetailState extends BaseMvvmState<EmergencyEventDetailViewModel,EmergencyEventDetailPage>{

  Widget get arrowRight => Container();

  @override
  Widget? buildLoadingContentView() {
    var detail = viewModel.detailInfo;
    return  SingleChildScrollView(
      child: Column(
        children: [

          SizedBox(height: 0,),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TitleValueWidget(title:'基本信息',value: '',showArrow: false,hint: '',head: Container(width: 3,height: 20,color: Colors.blue,)),
              Gaps.dividerLine,
              ...[
                TitleValueWidget(
                    title: '事件定性',
                    value: detail?.eventNatureName ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '事件类型',
                    value: detail?.eventTypeName ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '发生时间',
                    value: (detail?.startDate ?? "").changeTime(format: [yyyy,'-',mm,'-',dd]),
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '相关人类型',
                    value: detail?.relatedTypeName ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '相关人姓名',
                    value: detail?.name ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '联系方式',
                    value: detail?.contact ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '房间号',
                    value: detail?.roomNo ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '第一处理人',
                    value: detail?.firstHandler ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

                TitleValueWidget(
                    title: '发生地点',
                    value: detail?.place ?? "",
                    hint: '',
                    isRequired: false,
                    contentWeight: -1,
                    arrow: arrowRight),

              ].insertWidget( Gaps.dividerLine,)
            ],
          ).addToContainer(
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            padding: EdgeInsets.symmetric(horizontal: 16)
          ),
          
          Column(
            children: [
              TitleValueWidget(title: '详细信息', value: '',hint: '',showArrow: false,head: Container(width: 3,height: 20,color: Colors.blue,)),
              Column(
                children: [
                  TitleValueWidget(title: '事件经过', value: '',hint: '',showArrow: false,),
                  CommonText(detail?.incident??"").addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12)),
                  SizedBox(height: 12,)
                ],
              ),

              Column(
                children: [
                  TitleValueWidget(title: '处理措施', value: '',hint: '',showArrow: false,),
                  CommonText(detail?.measures??"").addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12)),
                  SizedBox(height: 12,)
                ],
              ),

              Column(
                children: [
                  TitleValueWidget(title: '产生后果', value: '',hint: '',showArrow: false,),
                  CommonText(detail?.consequence??"").addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12)),
                  SizedBox(height: 12,)
                ],
              ),

              Column(
                children: [
                  TitleValueWidget(title: '事件总结', value: '',hint: '',showArrow: false,),
                  CommonText(detail?.conclusion??"").addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12)),
                  SizedBox(height: 12,)
                ],
              ),

              Column(
                children: [
                  TitleValueWidget(title: '备注', value: '',hint: '',showArrow: false,),
                  CommonText(detail?.remark??"").addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12)),
                  SizedBox(height: 12,)
                ],
              )
            ].insertWidget(Gaps.dividerLine),
          )
              .addToContainer(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.symmetric(horizontal: 16)
          ),

          if((detail?.attachList??[]).isNotEmpty)
          Column(
            children: [
              TitleValueWidget(title: '附件', value: '',hint: '',showArrow: false,head: Container(width: 3,height: 20,color: Colors.blue,)),
              ...detail!.attachList!.map((e){
                return Row(
                  children: [
                    Image.asset(e.fileType.isImage?getImagePath('image_image',parent: 'assets'):getImagePath('image_file',parent: 'assets'),width: 35,height: 35,),
                    SizedBox(width: 10,),
                    CommonText(e.fileName).setWeight(1),
                    CommonText('查看',textColor: Colors.blue)
                  ],
                ).onTap((){
                  if(e.fileType.isImage){
                    print('${HttpApi.imageHead}${e.id}');
                    showBigImage(context, ['${HttpApi.imageHead}${e.id}']);
                  }else{

                  }
                });
              }).toList().insertWidget(SizedBox(height: 12,)),
              SizedBox(height: 12,)
            ],
          ).addToContainer(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.symmetric(horizontal: 16),
          ),
          if(detail?.status==0)
            TextButton(onPressed: () async {
              var data = await Go().push(EditEmergencyEventPage(id: detail?.id,));
              if(data!=null){
                Future.delayed(Duration(milliseconds: 300),(){
                  viewModel.getDetailInfo();
                });
              }
            }, child: CommonText('修改',textColor: Colors.white),style: badEventButtonStyle,).addToContainer(
              margin: EdgeInsets.symmetric(horizontal: 16)
            ),
          SizedBox(height: 20,)
        ].insertWidget(SizedBox(height: 12,)),
      ),
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('紧急事件详情'),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  EmergencyEventDetailViewModel createViewModel() {
    return EmergencyEventDetailViewModel(context,id: widget.id);
  }

  @override
  void onRetryClick() {
  }

}

extension FileTypeExt on String?{
  bool get isImage{
    var list = ['png','jpg','jpeg','gif',];
    return list.contains(this);
  }


}